import cv2 as cv
import numpy as np

def edge_demo(image):
    blurred = cv.pyrMeanShiftFiltering(image, 10, 15) # 均值迁移去噪声
    blurred = cv.GaussianBlur(blurred, (3, 3), 0)#高斯模糊
    # cv.imwrite("/Users/vine/Desktop/blurred.jpg", blurred)
    gray = cv.cvtColor(blurred, cv.COLOR_BGR2GRAY) #灰度转换
    xgrad = cv.Sobel(gray, cv.CV_16SC1, 1, 0)#计算梯度，（API要求不能为浮点数）
    ygrad = cv.Sobel(gray, cv.CV_16SC1, 0, 1)  
    # print(xgrad)
    # print("------------")
    # print(ygrad)
    edge_output = cv.Canny(xgrad, ygrad, 200, 280) #调用cv.Canny，利用高低阈值求出图像边缘
    cv.imwrite("/Users/vine/Desktop/edge_c.jpg", edge_output)


src = cv.imread("/Users/vine/Documents/Winglab/ISPTestCode/testPic/Clip_V1/2021091120354001191G-CL0615-6.jpg")
edge_demo(src)